<?php
namespace addons\baiduSpider\controller;

use think\Request;

use think\Controller;

class GlobalAction extends Controller
{

    public function baiduSpider()
    {
        
        $addonsInfo = config('addonsInfo');
        if (!$addonsInfo || !isset($addonsInfo['baiduSpider'])) {
            return false;
        }
        $userAgent = @Request::instance()->header()['user-agent'];
        if (strpos($userAgent,"Baiduspider") !== false) {
            if (strpos($userAgent,"Mobile") !== false) {
                if (strpos($userAgent,"render") !== false) {
                    db('AddonsSpiders')->insert(array('add_time' => time(),'type' => 'mobileRender','pageUrl' => $this->view->siteUrl, 'ip' => Request::instance()->ip(), 'vendor' => 'baidu'));
                } else {
                    db('AddonsSpiders')->insert(array('add_time' => time(),'type' => 'mobile','pageUrl' => $this->view->siteUrl, 'ip' => Request::instance()->ip(), 'vendor' => 'baidu'));
                }
            } else {
                if (strpos($userAgent,"render") !== false) {
                    db('AddonsSpiders')->insert(array('add_time' => time(),'type' => 'pcRender','pageUrl' => $this->view->siteUrl, 'ip' => Request::instance()->ip(), 'vendor' => 'baidu'));
                } else {
                    db('AddonsSpiders')->insert(array('add_time' => time(),'type' => 'pc','pageUrl' => $this->view->siteUrl, 'ip' => Request::instance()->ip(), 'vendor' => 'baidu'));
                }
            }
        	   $request = Request::instance();
        	   
        	if (strpos($request->routeInfo()['route'],'article/ArticleDetail/index') !== false) {
                $itemInfo = $this->view->itemInfo;
				db('Articles')->where('id',$itemInfo['id'])->update(array(
					'baidu_spider_num' => $itemInfo['baidu_spider_num'] + 1,
					'baidu_spider_time' => time(),
				));
				
			}

        }



    }

}

